package cn.imvc.app.controllers.seller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.StorageType;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.StorageTypeModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

/**
 * 
 * @author zhy
 * @date 2016年7月29日上午9:51:20
 *
 */
public class StorageTypeControllerImpl implements StorageTypeController{
	static Logger logger = Logger.getLogger(StorageTypeControllerImpl.class);
	public Page<StorageType> page;
	public ViewRequest viewRequest;
	public StorageType storageType;
	public int id;
	public int[] ids;
	public String search_content;
	
	@Autowired
	StorageTypeModel storageTypeModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		
		return null;
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
		}
		page = storageTypeModel.find(viewRequest);
		return "storage-type/index";
	}

	public String show() {
		storageType = storageTypeModel.get(id);
		return "storage-type/edit";
	}

	public String editNew() {
		
		return "storage-type/edit";
	}

	public String create() {
		int result = storageTypeModel.save(storageType);
		if(result==0){
			return Render.json(new DialogBean("300","仓库类型编码不能重复","storage-type","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","仓库类型名称不能重复","storage-type","closeCurrent"));
		}
		loggingModel.info(logger,"添加仓库类型："+storageType.getName());
		return Render.json(new DialogBean("200","创建成功","","closeCurrent"));
	}

	public String edit() {
		storageType = storageTypeModel.get(id);
		return "storage-type/edit";
	}

	public String update() {
		int result = storageTypeModel.update(id,storageType);
		if(result==0){
			return Render.json(new DialogBean("300","仓库类型编码不能重复","storage-type","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","仓库类型名称不能重复","storage-type","closeCurrent"));
		}
		loggingModel.info(logger,"修改仓库类型："+storageType.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		String result = storageTypeModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除仓库类型："+id);
			return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300", result,"")); 
		}
	}

	public String destroyBatch() {
		storageTypeModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功",""));
	}
/**
 * 外部调用
 * @return
 */
	public String dialog() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
		}
		page = storageTypeModel.find(viewRequest);
		return "storage-type/dialog";
	}
	
	public StorageType getStorageType() {
		return storageType;
	}

	public void setStorageType(StorageType storageType) {
		this.storageType = storageType;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

}
